<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>15 自定义iterator类</title>
</head>

<body>
  <script>
    class Iterator {
      constructor(array) {
        this.array = array
        this.index = 0
      }
      next() {
        if (this.index >= this.array.length) {
          return {
            done: true,
            value: 'undefined'
          }
        }
        return {
          done: false,
          value: this.array[this.index++]
        }
      }
    }

    let arr = [10, 20, 30]
    let ctor = new Iterator(arr)
    console.log(ctor.next()) //=> {done: false, value: 10}
    console.log(ctor.next()) //=> {done: false, value: 20}
    console.log(ctor.next()) //=> {done: false, value: 30}
    console.log(ctor.next()) //=> {done: true, value: undefined}
    console.log(ctor.next()) //=> {done: true, value: undefined}
  </script>
</body>

</html>